package servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.sql.*;

@WebServlet(name = "PersonServlet", value = "/PersonServlet")
public class PersonServlet extends HttpServlet {
    public void init(ServletConfig config)throws ServletException{
        super.init(config);
    }
    public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Connection conn=null;
        Statement sql=null;
        String userID=request.getParameter("UserID");
        String Tel=request.getParameter("Tel");
        String Email=request.getParameter("Email");
//        String Name=request.getParameter("Name");
        String Sex=request.getParameter("Sex");
        String Birth=request.getParameter("Birth");
        String oldPassword=request.getParameter("oldPassword");
        String newPassword=request.getParameter("newPassword");
        try {
            if (oldPassword==null||oldPassword.equals("")){

            }else {
            oldPassword=Md5Utils.encoder(oldPassword);}
            if (newPassword==null||oldPassword.equals("")){

            }else {
                newPassword=Md5Utils.encoder(newPassword);
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        String Password = null;
        String driver="org.mariadb.jdbc.Driver";
        String url="jdbc:mariadb://localhost:3306/hotel-management-system";
        try {
            Class.forName(driver);
            conn=DriverManager.getConnection(url,"root","123456");
            sql=conn.createStatement();
            ResultSet rs;
            String ex="select Password from users where UserID='"+userID+"'";
            rs=sql.executeQuery(ex);
            rs.beforeFirst();
            while (rs.next()){
                Password=rs.getString("Password");
            }
            System.out.println("PersonServlet53"+Password);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        if (oldPassword==null||newPassword==null||newPassword.equals("")||oldPassword.equals("")){
        }else if (oldPassword.equals(Password)&&!newPassword.equals("")){
            Password=newPassword;
        }

        try {
            Class.forName(driver);
            conn= DriverManager.getConnection(url,"root","123456");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        try {
//        con=ds.getConnection();
            assert conn != null;
            sql=conn.createStatement();
            //读取酒店详情表
            String ex="update users set Tel='"+Tel+"',Email='"+Email+"',Sex='"+Sex+"',Birth='"+Birth+"',Password='"+Password+"' where UserID='"+userID+"'";
            sql.executeQuery(ex);
        }catch (SQLException e){
            System.out.print(e);
        }
        finally {
            try {
                assert conn != null;
                conn.close();
            }catch (Exception e){
                System.out.print(e);
            }
        }
        response.sendRedirect("person.jsp");
    }
}
